package com.lrkj.framework.utils;


import com.lrkj.common.utils.spring.SpringUtils;
import com.lrkj.system.domain.SysSerialNum;
import com.lrkj.system.service.ISysSerialNumService;

import java.util.HashMap;
import java.util.Map;

public class SerialNumUtil {

    private static SerialNumUtil INSTANCE = null;

    private Map<String, SerialNumber> keyMap;
    private ISysSerialNumService serialNumSerivce;

    private SerialNumUtil() {
        keyMap = new HashMap<String, SerialNumber>();
        serialNumSerivce = (ISysSerialNumService) SpringUtils.getBean("sysSerialNumService");
    }

    public static SerialNumUtil getInstance() {
        if (INSTANCE == null) {
            synchronized (SerialNumUtil.class) {
                if (INSTANCE == null) {
                    INSTANCE = new SerialNumUtil();
                }
            }
        }
        return INSTANCE;
    }

    public synchronized int nextInt(String key) {
        return new Long(nextLong(key)).intValue();
    }

    public long nextLong(String key) {
        SerialNumber serialNumber;
        synchronized (keyMap) {
            if (keyMap.containsKey(key)) {
                serialNumber = keyMap.get(key);
            } else {
                serialNumber = new SerialNumber(key);
                keyMap.put(key, serialNumber);
            }
        }
        return serialNumber.nextLong();
    }

    public class SerialNumber {

        private long serialNum;
        private String key;
        private int seed;

        public SerialNumber(String key) {
            this.key = key;
            this.seed = 1;
            init();
        }

        protected void init() {
            SysSerialNum eg = findEg();
            if (eg != null) {
                serialNum = eg.getSn();
                eg.setSn(serialNum + seed);
            } else {
                eg = new SysSerialNum();
                eg.setSnKey(key);
                eg.setSn((long) seed);
            }
            serialNumSerivce.doSaveOrUpdate(eg);
        }

        private SysSerialNum findEg() {
            return serialNumSerivce.selectSysSerialNumByKey(key);
        }

        public synchronized long nextLong() {
            if (seed == 0) {
                seed = 1;
                SysSerialNum eg = findEg();
                if(null != eg){
                    serialNum = eg.getSn();
                    eg.setSn(serialNum + seed);
                }else{
                    eg = new SysSerialNum();
                    eg.setSnKey(key);
                    eg.setSn(Long.valueOf(seed));
                }
                serialNumSerivce.doSaveOrUpdate(eg);
            }
            serialNum++;
            seed--;
            return serialNum;
        }
    }


    //获取编号
    public static String getNo(String key) {
        int nextVal = SerialNumUtil.getInstance().nextInt(key);
        return key + NumberUtils.frontCompWithZore(nextVal, 7);
    }

    //获取编号
    public static String getNo(String key,int formatLength) {
        int nextVal = SerialNumUtil.getInstance().nextInt(key);
        return key + NumberUtils.frontCompWithZore(nextVal, formatLength);
    }

}
